package com.bm.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bm.server.config.RedisKeyConfig;
import com.bm.server.dto.RoomUerDto;
import com.bm.server.util.JedisUtil;
import com.bm.server.util.Rutils;
import com.bm.server.vo.R;
import com.bm.server.dao.*;
import com.bm.server.entity.*;
import com.bm.server.service.intf.RoomUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

;

/**
 * YoungMan, may the force be with you.
 *
 * @Author: 吃个小布丁吧
 * @Date: 2022/07/20/21:20
 */
@Service
public class RoomUserServiceImpl implements RoomUserService {
    @Resource
    private RoomUserDao dao;
    @Resource
    private RoominfoDao infoDao;
    @Resource
    private UnderDao underDao;
    @Resource
    private PingDao pingDao;
    @Resource
    private UnderCoverDao coverDao;
    @Override
    public R insert(RoomUerDto dto) {
        if (dto!=null) {
            QueryWrapper<RoomUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("roomid", dto.getRoomid());
            List<RoomUser> list = dao.selectList(queryWrapper);
            if (list.size() < 8) {
                RoomUser roomUser = new RoomUser();
                roomUser.setRoomid(dto.getRoomid());
                roomUser.setNameid(dto.getNameid());
                roomUser.setType(dto.getType());
                roomUser.setUserhead(dto.getUserhead());
                dao.insert(roomUser);
                return Rutils.ok("进来了");
            }
        }
        return Rutils.fail("房间人数满了 换个房");
    }

    @Override
    public R select(int roomid) {
        return Rutils.ok(dao.select(roomid));
    }

    @Override
    public R del(Integer uid, Integer roomid) {
        if (uid!=null&&roomid!=null){
            List<RoomUser> roomUsers = dao.selectAll(roomid, uid);
            if (roomUsers.get(0)!=null){
                if(dao.deleteById(roomUsers.get(0))>0){
                    return Rutils.ok();
                }
            }
        }
        return Rutils.fail();
    }

    @Override
    public R randm() {
        Undercover undercover = coverDao.radem();
        return R.ok(undercover);
    }
    @Transactional
    @Override
    public R getTwo(Integer uid, Integer roomid) {
        if(uid!=null&&roomid!=null){
            if (underDao.selectList(null).size()>0){
                underDao.delall();
            }
            List<Rootinfo> rootinfos = infoDao.selectAll(roomid, uid);
            if (rootinfos.size()>0){
                ArrayList<Integer> list = new ArrayList<>();
                List<RoomUser> two = dao.getTwo(roomid);
                for (RoomUser roomuser:two) {
                    list.add(roomuser.getNameid());
                    underDao.insert(new Under(roomid,roomuser.getNameid()));
                    JedisUtil.addHash(RedisKeyConfig.UNDER_UID+roomid,roomuser.getNameid()+"","");
                }
                return Rutils.ok(list);
            }
        }
        return Rutils.fail();
    }
    @Transactional
    @Override
    public R getfour(Integer uid, Integer roomid) {
        if(uid!=null&&roomid!=null){
            if (pingDao.selectList(null).size()>0){
                pingDao.delall();
            }
            if (underDao.selectList(null).size()==0){
                getTwo(uid,roomid);
            }
                List<RoomUser> roomUsers = dao.selectList(new QueryWrapper<RoomUser>().eq("roomid", roomid));
                ArrayList<Integer> list = new ArrayList<>();
                for (RoomUser roomuser:roomUsers) {
                    if (!JedisUtil.checkHash(RedisKeyConfig.UNDER_UID+roomid,roomuser.getNameid()+"")){
                        list.add(roomuser.getNameid());
                        pingDao.insert(new Ping(roomid,roomuser.getNameid()));
                    }
                }
                return Rutils.ok(list);
        }
        return Rutils.fail();
    }
    @Transactional
    @Override
    public R play(Integer uid) {
        if (uid>0){
            Under under = underDao.selectOne(new QueryWrapper<Under>().eq("uid", uid));
            Ping ping = pingDao.selectOne(new QueryWrapper<Ping>().eq("uid", uid));
            if (under!=null){
                if (underDao.deleteById(under)>0){
                    JedisUtil.delhashKeys(RedisKeyConfig.UNDER_UID+under.getRoomid(),uid+"");
                    if (underDao.selectList(null).size()==0){
                        if (pingDao.delall()>0){
                            return Rutils.ok("平民获胜",null);
                        }
                    }
                }
            }else {
                if (pingDao.deleteById(ping)>0){
                    if (pingDao.selectList(null).size()==0){
                        if (underDao.delall()>0) {
                            return Rutils.ok("卧底获胜", null);
                        }
                    }
                }
            }
        }
        return Rutils.ok("游戏继续");
    }


}
